package com.irssproject.app.web.action.main;

import java.util.Map;

import com.irssproject.app.entitats.User;
import com.irssproject.app.service.main.iface.UsersService;
import com.irssproject.app.utils.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.logging.Logger;

public class UserAction extends ActionSupport {

    private static final long serialVersionUID = -3184393109770695603L;
    private String password;
    private String pais;
    private String nom;
    private String email;
    UsersService usuariService;


    Logger logger = Logger.getLogger(UserAction.class.getName());

    public UserAction(UsersService usuariService) {
        this.usuariService = usuariService;
    }

    public String getPais() {
        return pais;
    }

    public void setPais(String pais) {
        this.pais = pais;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String donarDeBaixaUsuari() {

        try {

            Map<String, Object> session = ActionContext.getContext().getSession();
            String login = ((User) session.get(Constants.SESSIO_USUARI_AUTENTICAT)).getLogin();


            if (session.containsKey(Constants.SESSIO_USUARI_AUTENTICAT)) {
                User usuari = new User();
                usuari.setLogin(login);
                usuari.setPassword(password);
                User usr = usuariService.login(usuari.getLogin(), usuari.getPassword());

                if (usr != null) {
                    usuariService.deleteUser(usr);
                } else {
                    addActionError(getText("error.usuariaction.autenticacio"));
                    return INPUT;
                }


            }

        } catch (Exception e) {
            logger.info(e.getLocalizedMessage());
        }

        return SUCCESS;
    }

    public String editarUsuari() {

        try {

            Map<String, Object> session = ActionContext.getContext().getSession();
            String login = ((User) session.get(Constants.SESSIO_USUARI_AUTENTICAT)).getLogin();

            if (session.containsKey(Constants.SESSIO_USUARI_AUTENTICAT)) {


                User usuari = usuariService.login(login, password);

                if (usuari != null) {
                    if ("".equals(nom) || "".equals(email)) {

                        if ("".equals(nom)) {
                            addActionError(getText("error.esbuit.nom"));
                        }
                        if ("".equals(email)) {
                            addActionError(getText("error.esbuit.email"));
                        }

                        return INPUT;
                    } else {
                        usuari.setName(nom);
                        usuari.setPlace(pais);
                        usuari.setEmail(email);
                        usuariService.updateUser(usuari);

                        session.put(Constants.SESSIO_USUARI_AUTENTICAT, usuari);

                    }


                } else {
                    addActionError(getText("error.usuariaction.autenticacio"));
                    return INPUT;
                }
            }


        } catch (Exception e) {
            logger.info(e.getLocalizedMessage());

        }

        return SUCCESS;
    }
}
